Skip to content

feat: Add FeatureMessage, MessageBus, and a messaging thread#180

Merged
awforsythe merged 4 commits into
mainfrom
aforsythe/RUM-12206/message-bus
Mar 24, 2026
Merged

feat: Add FeatureMessage, MessageBus, and a messaging thread#180
awforsythe merged 4 commits into
mainfrom
aforsythe/RUM-12206/message-bus

Conversation

@awforsythe

@awforsythe awforsythe commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

refs: RUM-12206

This PR adds a MessageBus to the Core, along with a new dedicated background thread: the messaging thread.

Rationale

For the typical work done by features (e.g. reading from CoreContext and generating events), the existing context thread is sufficient: a feature enqueues its event-generation function to be run on the context thread, and that function will be invoked with an immutable, thread-safe copy of the CoreContext. This ensures that when a feature has decided to produce an event, it always sees a consistent, up-to-date snapshot of the CoreContext.

However, we still need a non-lazy mechanism of propagating CoreContext changes to features that need to eagerly perform work in response to those changes. This same mechanism can be used for other state changes that need to proactively signal other features. The MessageBus is designed with two use cases in mind:

  1. The CrashReporting feature needs to respond to changes in RUM context etc., conveying the new values to its crash handler implementation, such that they can be a.) eagerly flushed to disk alongside the crash report file (inprocess), or b.) stored as annotations to be read by an external process in the result of a crash (crashpad).

  2. The Rum feature needs to respond to critical errors recorded via Logging (when configured to do so), automatically recording a RUM Error in response to the log call.

This PR does not attempt to implement these feature-specific changes; it is concerned only with the defining the net-new types and functions required by the Core to support messaging.

Scope of this PR

RUM-12206 represents the Core SDK changes required to support core-to-feature and feature-to-feature messaging, not including any feature-specific changes.

The implementation of RUM-12206 is split into three phases:

  1. Defining message types, implementing the message bus and the messaging thread
  2. Installing those new components into the Core; updating Feature with the ability to register itself with the message bus and respond to messages (enabling the CrashReporting use case described above)
  3. Updating Feature with the ability to send messages on the message bus (enabling the Logging->Rum use case described above) - this may be a good point to decide on whether features should explicitly subscribe on a per-message-type basis to limit broadcast overhead.

This PR represents the completion of Phase 1.

Changes in this PR

  • Add FeatureMessage, a std::variant that currently just consists of ContextChangedMessage
  • Add MessageBus, which wraps a Queue of FeatureMessage and a list of registered handlers, represented as std::function callbacks
  • Add MessagingThreadMain, which consumes from the queue, invoking all registered handlers for each message consumed
  • Add unit tests for MessageBus
  • Add basic unit tests for the high-level behavior of MessagingThreadMain

This pattern is consistent with how the SDK currently manages all
Core-owned background threads: for the sake of clarity, each dedicated
background thread gets its own header (e.g. `messaging_thread.hpp`) and
its own consistently-named entry point (e.g. `MessagingThreadMain`), and
the Core explicitly manages the lifetime of each thread, rather than
adding layers of indirection to thread startup/shutdown logic by
encapsulating them in other classes.

In other words, data (e.g. `MessageBus`) is kept entirely separate from
the Core-owned `std::thread` that runs the thread's logic (e.g.
`MessagingThreadMain`).

If we change this pattern, we should do so consistently for all threads'
implementations.
@awforsythe

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@awforsythe awforsythe marked this pull request as ready for review March 13, 2026 13:06
@awforsythe awforsythe requested a review from a team as a code owner March 13, 2026 13:06
Base automatically changed from aforsythe/RUM-13164/context-queue to main March 13, 2026 18:56
@awforsythe awforsythe requested a review from a team as a code owner March 13, 2026 18:56
private:
friend void MessagingThreadMain(const DiagnosticLogger&, MessageBus&);

Queue<FeatureMessage> _queue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably should have said this back when this was first created but Queue seems like too simple a name for everything this does....

@awforsythe awforsythe merged commit beee25c into main Mar 24, 2026
18 checks passed
@awforsythe awforsythe deleted the aforsythe/RUM-12206/message-bus branch March 24, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants